import store from '../store/index.js';

var ipAddress;
if (process.env.NODE_ENV === 'development') {
	// ipAddress = "https://api.volcore.xyz/"
	ipAddress = 'http://192.168.11.8:8883/';
	// ipAddress = 'http://erp.qhenv.com:9993/';
} else {
	ipAddress = 'http://erp.qhenv.com:9993/' ;
}
if (!uni.getStorageInfoSync().keys.includes('IP')) {
	store.commit('setIP', ipAddress);
}
function post(url, data, loading) {
	return request(url, 'POST', data, loading);
}

async function get(url, data, loading) {
	return request(url, 'GET', data, loading);
}

function getToken() {
	return store.getters.getToken();
}

function request(url, method, data, loading) {
	if (loading) {
		uni.showLoading({
			title: typeof loading == 'boolean' ? '正在处理...' : loading
		});
	}
	if (url.startsWith('/')) {
		url = url.substr(1);
	}
	// url = ipAddress + url;
	url = (store.state.IP || ipAddress) + url;
	var _header = {};
	var _token = getToken();
	if (_token) {
		_header['Authorization'] = _token;
	}
	// 携带版本信息
	const appVersion = uni.getSystemInfoSync().appVersion;
	console.log(appVersion);
	_header['App_version']=appVersion
	// 携带版本信息end
	return new Promise((reslove, reject) => {
		_header.uapp = '1';
		uni.request({
			url: url,
			method: method,
			data: data,
			header: _header,
			timeout:50000,
			success: (res) => {
				console.log('res.', res);
				if (loading) {
					uni.hideLoading();
				}
				if (res.statusCode == 500) {
					// console.log(JSON.stringify(res));
					uni.showToast({
						icon: 'none',
						title: '服务器内部错误'
					});
					return;
				}
				if (res.statusCode == 404) {
					uni.showToast({
						icon: 'none',
						title: '未找到请求接口'
					});
					return;
				}
				if (res.statusCode == 403) {
					console.log('清除userInfo,返回登录页');
					store.commit('clearUserInfo');
					const currentPage = getCurrentPages();
					const currentRoute = currentPage[currentPage.length-1].route;
					if(currentRoute!="/pages/login/login"){
						uni.reLaunch({
							url: '/pages/login/login'
						});
					}
					uni.showToast({
						icon: 'none',
						title: '当前版本已暂停服务,请联系管理员获取最新版本'
					});
					return;
				}
				if (res.statusCode == 202 || res.statusCode == 401) {
					if (res.data && res.data.message && res.data.code != '401') {
						uni.showToast({
							icon: 'none',
							title: res.data.message
						});
						return;
					}
					console.log('清除userInfo,返回登录页');
					store.commit('clearUserInfo');
					uni.showToast({
						icon: 'none',
						title: '登录过期，请重新登录',
						duration:2000
					});
					uni.reLaunch({
						url: '/pages/login/login'
					});
					return;
				}

				if (res.header.vol_exp == '1') {
					post('api/User/replaceToken', 'POST').then(async (result) => {
						let userInfo = store.getters.getUserInfo();
						userInfo.token = result.data;
						store.commit('setUserInfo', userInfo);
					});
				}
				reslove(res.data);
			},
			fail: (err) => {
				if (loading) {
					uni.hideLoading();
				}
				// console.log('err', err);
				// console.log("url",url);
				// store.commit('clearUserInfo');
				if ((err.hasOwnProperty('statusCode') && err.statusCode == 401) || (err.data && err.data.code == 401)) {
					uni.reLaunch({
						url: '/pages/login/login'
					});
					return;
				}
				// if(url!='/pages/login/login'){
				// 	uni.reLaunch({
				// 		url: '/pages/login/login'
				// 	});
				// }
				uni.showToast({
					icon: 'none',
					title: '1请求接口失败' + JSON.stringify(err)
				});
			}
		});
	});
}

export default {
	get,
	post,
	request,
	ipAddress
};
